package com.gimi.cloud.bbp.commonService.InterfacePlatform.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.gimi.cloud.bbp.common.constant.RedisKeyConstant;
import com.gimi.cloud.bbp.common.utils.HttpClientUtil;
import com.gimi.cloud.bbp.commonService.InterfacePlatform.InterfacePlatformTokenService;
import com.gimi.cloud.bbp.commonService.redis.RedisService;
import com.gimi.cloud.bbp.dao.dao.sys.SysConfigDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


@Service
public class InterfacePlatformTokenServiceImpl implements InterfacePlatformTokenService {

	private static final Logger log = LoggerFactory.getLogger(InterfacePlatformTokenServiceImpl.class);

	private final String INTERFACE_PLATFORM_UNIVERSAL="interface_platform_universal";
	@Autowired
	private SysConfigDao sysConfigDao;
	@Autowired
    private RedisService redisService;



	@Override
	public String getInterfacePlatformToken() throws Exception {
		log.info("获取TOKEN");

		log.info("获取接口平台通用配置");
		String interfacePlatformUniversal = sysConfigDao.getConfigByKey(INTERFACE_PLATFORM_UNIVERSAL);
		JSONObject ipuconfig = JSON.parseObject(interfacePlatformUniversal);
		String domain = ipuconfig.getString("domain");
		JSONObject tokenConfig = ipuconfig.getJSONObject("token");
		String tokenUrl = tokenConfig.getString("url");
		String salt = tokenConfig.getString("salt");
		String data = tokenConfig.getString("data");

		log.info("组装请求头");
		BasicHeader basicHeader = new BasicHeader("salt", salt);
		List<Header> headerLists = new ArrayList<>();
		headerLists.add(basicHeader);
		Header[] headers = headerLists.toArray(new Header[headerLists.size()]);

		log.info("组装参数");
		Map<String, String> paramMap = new HashMap<>();
		paramMap.put("data", data);
		
		log.info("发送请求");
		String resp = HttpClientUtil.sendPostParam(domain + tokenUrl, paramMap, headers);
		JSONObject respJson = JSON.parseObject(resp);
		String token = respJson.getJSONObject("body").getString("token");
		//token前加任意七个字符
		token = "jimiiii"+token;
		try {
			//存放23小时候会过期,过期了去数据库拿
			redisService.set(RedisKeyConstant.INTERFACE_PLATFORM_TOKEN,token,23L, TimeUnit.HOURS);
		} catch (Exception e) {
			log.error("TOKEN保存到Redis错误:"+e);
			e.printStackTrace();
		}
		return token;
	}

	@Override
	public String getTokenInRedis() throws Exception {
		String InterfacePlatformToken = null;
		try {
			InterfacePlatformToken = redisService.get(RedisKeyConstant.INTERFACE_PLATFORM_TOKEN);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (StringUtils.isBlank(InterfacePlatformToken)) {
			InterfacePlatformToken = getInterfacePlatformToken();
		}
		return InterfacePlatformToken;
	}

	@Override
	public String getApiUrl() throws Exception{
		String interfacePlatformUniversal = sysConfigDao.getConfigByKey(INTERFACE_PLATFORM_UNIVERSAL);
		JSONObject ipuconfig = JSON.parseObject(interfacePlatformUniversal);
		String domain = ipuconfig.getString("domain");
		String apiUrl = ipuconfig.getJSONObject("api").getString("url");
		return domain+apiUrl;
	}
	
	@Override
	public Header[] getCarryTokenHeader() throws Exception {
		String token = null;
		token = getTokenInRedis();
		BasicHeader basicHeader = new BasicHeader("Authorization", token);
		List<Header> headerLists = new ArrayList<>();
		headerLists.add(basicHeader);
		return headerLists.toArray(new Header[headerLists.size()]);
	}

}
